home *** CD-ROM | disk | FTP | other *** search
/ Java Developer's Companion / Java Developer's Companion.iso / binaries / Windows / BDK / apis / java / beans / VetoableChangeSupport.java < prev    next >
Encoding:
Java Source  |  1997-03-17  |  4.9 KB  |  165 lines

  1. /*
  2.  * @(#)VetoableChangeSupport.java    1.12 97/02/18  
  3.  * 
  4.  * Copyright (c) 1996 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  * CopyrightVersion bdk_beta
  20.  * 
  21.  */
  22.  
  23. package java.beans;
  24.  
  25. import java.io.Serializable;
  26. import java.io.ObjectOutputStream;
  27. import java.io.ObjectInputStream;
  28. import java.io.IOException;
  29.  
  30.  
  31. /**
  32.  * This is a utility class that can be used by beans that support constrained
  33.  * properties.  Your can either inherit from this class or you can use
  34.  * an instance of this class as a member field of your bean and delegate
  35.  * various work to it.
  36.  */
  37.  
  38. public class VetoableChangeSupport implements java.io.Serializable {
  39.  
  40.     /**
  41.      * @sourceBean  The bean to be given as the source for any events.
  42.      */
  43.  
  44.     public VetoableChangeSupport(Object sourceBean) {
  45.     source = sourceBean;
  46.     }
  47.  
  48.     /**
  49.      * Add a VetoableListener to the listener list.
  50.      *
  51.      * @param listener  The VetoableChangeListener to be added
  52.      */
  53.  
  54.     public synchronized void addVetoableChangeListener(
  55.                     VetoableChangeListener listener) {
  56.     if (listeners == null) {
  57.         listeners = new java.util.Vector();
  58.     }
  59.     listeners.addElement(listener);
  60.     }
  61.  
  62.     /**
  63.      * Remove a VetoableChangeListener from the listener list.
  64.      *
  65.      * @param listener  The VetoableChangeListener to be removed
  66.      */
  67.     public synchronized void removeVetoableChangeListener(
  68.                     VetoableChangeListener listener) {
  69.     if (listeners == null) {
  70.         return;
  71.     }
  72.     listeners.removeElement(listener);
  73.     }
  74.  
  75.     /**
  76.      * Report a vetoable property update to any registered listeners.  If
  77.      * anyone vetos the change, then fire a new event reverting everyone to 
  78.      * the old value and then rethrow the PropertyVetoException.
  79.      * <p>
  80.      * No event is fired if old and new are equal and non-null.
  81.      *
  82.      * @param propertyName  The programmatic name of the property
  83.      *        that was changed.
  84.      * @param oldValue  The old value of the property.
  85.      * @param newValue  The new value of the property.
  86.      * @exception PropertyVetoException if the recipient wishes the property
  87.      *              change to be rolled back.
  88.      */
  89.     public void fireVetoableChange(String propertyName, 
  90.                     Object oldValue, Object newValue)
  91.                     throws PropertyVetoException {
  92.  
  93.     if (oldValue != null && oldValue.equals(newValue)) {
  94.         return;
  95.     }
  96.  
  97.     java.util.Vector targets;
  98.     synchronized (this) {
  99.         if (listeners == null) {
  100.             return;
  101.         }
  102.         targets = (java.util.Vector) listeners.clone();
  103.     }
  104.         PropertyChangeEvent evt = new PropertyChangeEvent(source,
  105.                         propertyName, oldValue, newValue);
  106.  
  107.     try {
  108.         for (int i = 0; i < targets.size(); i++) {
  109.             VetoableChangeListener target = 
  110.                 (VetoableChangeListener)targets.elementAt(i);
  111.             target.vetoableChange(evt);
  112.         }
  113.     } catch (PropertyVetoException veto) {
  114.         // Create an event to revert everyone to the old value.
  115.                evt = new PropertyChangeEvent(source, propertyName, newValue, oldValue);
  116.         for (int i = 0; i < targets.size(); i++) {
  117.         try {
  118.                 VetoableChangeListener target =
  119.                 (VetoableChangeListener)targets.elementAt(i);
  120.                 target.vetoableChange(evt);
  121.         } catch (PropertyVetoException ex) {
  122.              // We just ignore exceptions that occur during reversions.
  123.         }
  124.         }
  125.         // And now rethrow the PropertyVetoException.
  126.         throw veto;
  127.     }
  128.     }
  129.  
  130.     private void writeObject(ObjectOutputStream s) throws IOException {
  131.         s.defaultWriteObject();
  132.  
  133.     java.util.Vector v = null;
  134.     synchronized (this) {
  135.         if (listeners != null) {
  136.             v = (java.util.Vector) listeners.clone();
  137.             }
  138.     }
  139.  
  140.     if (v != null) {
  141.         for(int i = 0; i < listeners.size(); i++) {
  142.             VetoableChangeListener l = (VetoableChangeListener)v.elementAt(i);
  143.             if (l instanceof Serializable) {
  144.                 s.writeObject(l);
  145.             }
  146.             }
  147.         }
  148.         s.writeObject(null);
  149.     }
  150.  
  151.  
  152.     private void readObject(ObjectInputStream s) throws ClassNotFoundException, IOException {
  153.         s.defaultReadObject();
  154.       
  155.         Object listenerOrNull;
  156.         while(null != (listenerOrNull = s.readObject())) {
  157.         addVetoableChangeListener((VetoableChangeListener)listenerOrNull);
  158.         }
  159.     }
  160.  
  161.     transient private java.util.Vector listeners;
  162.     private Object source;
  163.     private int vetoableChangeSupportSerializedDataVersion = 1;
  164. }
  165.